#include "bflb_mtimer.h"
#include "board.h"
#include "bflb_uart.h"
#include "bflb_gpio.h"
#include "FreeRTOS.h"
#include "task.h"

#define DBG_TAG "MAIN"
#include "log.h"

struct bflb_device_s *rd03d;
uint8_t UART_RECEIVE_BUFFER[32];

void _rd03dTask(void* pvParameters){
    while (1)
    {
        memset(UART_RECEIVE_BUFFER, 0, sizeof(UART_RECEIVE_BUFFER));
     if(bflb_uart_rxavailable(rd03d))
        {
            int len = bflb_uart_get(rd03d, UART_RECEIVE_BUFFER, 32);
            if(len > 30){
                 for (size_t i = 0; i < sizeof(UART_RECEIVE_BUFFER); i++)
                {
                    LOG_E("0x%2x", UART_RECEIVE_BUFFER[i]);
                }
                LOG_E("\r\n");
            }

        }
       
    }
    vTaskDelay(3000/ portTICK_RATE_MS);
}

int main(void)
{
    board_init();

    struct bflb_device_s * gpio = bflb_device_get_by_name("gpio");
    bflb_gpio_uart_init(gpio, GPIO_PIN_23, GPIO_UART_FUNC_UART1_TX);
    bflb_gpio_uart_init(gpio, GPIO_PIN_24, GPIO_UART_FUNC_UART1_RX);

    rd03d = bflb_device_get_by_name("uart1");

    struct bflb_uart_config_s conf = {
        .baudrate = 256000,
        .data_bits = UART_DATA_BITS_8,
        .stop_bits = UART_STOP_BITS_1,
        .parity = UART_PARITY_NONE,
        .flow_ctrl = UART_FLOWCTRL_NONE,
        .rx_fifo_threshold = 7,
        .tx_fifo_threshold = 7
    };
    bflb_uart_init(rd03d, &conf);

    xTaskCreate(_rd03dTask, (char*)"rd03d", 1024, NULL, 1, NULL);
    vTaskStartScheduler();

    while (1) {
     
    }
}
